using System;

using Vibstudio.X4NET.IO.INI.Model.Configuration;
using Vibstudio.X4NET.IO.INI.Model.Formatting;

namespace Vibstudio.X4NET.IO.INI.Model
{
    #region Namespaces

    

    #endregion

    public class IniData : ICloneable
    {
        #region Non-Public Members

        private SectionDataCollection _sections;

        #endregion

        #region Initialization
        
        public IniData() : this (new SectionDataCollection())
        {
        }

        public IniData(SectionDataCollection sdc)
        {
            _sections = (SectionDataCollection)sdc.Clone();
            Global = new KeyDataCollection();
        }

        public IniData(IniData ori): this(ori.Sections)
        {
            Global = (KeyDataCollection) ori.Global.Clone();
            Configuration = ori.Configuration.Clone();
        }

        #endregion

        #region Properties

        public IIniParserConfiguration Configuration
        {
            get
            {
                // Lazy initialization
                return _configuration ?? (_configuration = new DefaultIniParserConfiguration());
            }

            set { _configuration = value.Clone(); }
        }

        public KeyDataCollection Global { get; private set; }

        public KeyDataCollection this[string sectionName]
        {
            get { return _sections.ContainsSection(sectionName) ? _sections[sectionName] : null; }
        }

        public SectionDataCollection Sections
        {
            get { return _sections; }
            set { _sections = value; }
        }

        #endregion

        #region Object Methods

        public override string ToString()
        {
            return ToString(new DefaultIniDataFormatter(Configuration));
        }
        
       
        public virtual string ToString(IIniDataFormatter formatter)
        {
            return formatter.IniDataToString(this);
        }
        
        #endregion

        #region ICloneable Members

        public object Clone()
        {
            return new IniData(this);
        }

        #endregion

        #region Fields

        private IIniParserConfiguration _configuration;

        #endregion
    }
} 